<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:qrCode:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:qrCode:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:qrCode:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:qrCode:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qrCodeList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="二维码" align="center" prop="qrUrl" >
        <template slot-scope="scope">
          <img :src="scope.row.qrUrl" alt="二维码" height="50px" width="50px"  v-if="scope.row.qrUrl" @click="showImg(scope.row.qrUrl)">
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="remarks" />
      <el-table-column label="位置" align="center" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-place"
            @click="handleShowPosition(scope.row)"
          >查看位置</el-button>
        </template>
      </el-table-column>
      <el-table-column label="公司" align="center" prop="company" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:qrCode:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:qrCode:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 二维码详情 -->
    <el-dialog :title="imgTitle" :visible.sync="imgOpen" v-if="imgOpen"  width="30%" append-to-body>
      <show-imgs :imgs="imgUrl"></show-imgs>
    </el-dialog>
    <!-- 添加或修改二维码管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="125px">
        <el-form-item label="名称" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="经纬度" prop="">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-location"
            @click="handleLocation"
          >定位</el-button>
        </el-form-item>
        <el-form-item label="经度" prop="gpsx">
          <el-input v-model="form.gpsx" readonly placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="gpsy">
          <el-input v-model="form.gpsy" readonly placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="公司" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司" />
        </el-form-item>
        <el-form-item label="允许使用年限" prop="years">
          <el-input v-model="form.years" placeholder="请输入允许使用年限">
            <template #append>年</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 打开地图 -->
    <div v-if="openMap" style="width: 60%">
      <TXMap @cancel="openMap = false" @map-confirm="confirmLocation" @getPlace="getPlace" :visible="openMap" :position="position"></TXMap>
    </div>

  </div>
</template>

<script>
import { listQrCode, getQrCode, delQrCode, addQrCode, updateQrCode, exportQrCode } from "@/api/system/qrCode";
import ShowImgs from "@/components/ShowImgs";
import TXMap from '@/components/TXMap';
export default {
  name: "QrCode",
  components: {
    ShowImgs,
    TXMap
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 二维码管理表格数据
      qrCodeList: [],

      // 弹出层标题
      title: "",
      imgOpen: false,
      imgTitle: "",
      imgUrl: "",
      // 是否显示弹出层
      open: false,
      //打开地图
      openMap: false,
      //自动定位的经纬度
      position: this.getLatAndLng(),
      // 状态字典
      statusOptions: [],
      longitudeLatitude:{},//地图经纬度
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        remarks: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("qr_code_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询二维码管理列表 */
    getList() {
      this.loading = true;
      listQrCode(this.queryParams).then(response => {
        this.qrCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.imgOpen = false;
      this.openMap = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        qrUrl: undefined,
        communityId: undefined,
        remarks: undefined,
        gpsy: undefined,
        gpsx: undefined,
        company: undefined,
        status: undefined,
        years: undefined,
        createtime: undefined,
        updatetime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //查看封面图
    showImg(imgUrl) {
      this.imgOpen = true;
      this.imgTitle = "二维码详情";
      this.imgUrl = imgUrl;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加二维码管理";
    },
    /** 打开地图 */
    handleLocation() {
      this.openMap = true;
      this.title = "经纬度选择";
    },
    confirmLocation() {
      const e = this.longitudeLatitude
      this.form.gpsy = parseFloat(e.lat).toFixed(6);
      this.form.gpsx = parseFloat(e.lng).toFixed(6);
      this.locationAddress = e.addr
      this.form.address = e.addr
      this.openMap = false
    },
    getPlace (e) {
      this.longitudeLatitude = e
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getQrCode(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.position={
          latitude:this.form.gpsy,
          longitude:this.form.gpsx,
        }
        this.open = true;
        this.title = "修改二维码管理";
      });
    },
    /** 查看位置 */
    handleShowPosition(row) {
      this.position={
        latitude:row.gpsy,
        longitude:row.gpsx,
      }
      this.openMap = true;
      this.title = "二维码位置";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateQrCode(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQrCode(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除二维码管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delQrCode(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有二维码管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportQrCode(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
